/*
 * task_communication.h
 *
 *  Created on: Jul 23, 2024
 *      Author: citunhk
 */

#ifndef TASK_COMMUNICATION_H_
#define TASK_COMMUNICATION_H_

#include "../../03_FML/MODBUS/Inc/agile_modbus.h"
#include "../../03_FML/OS/fml_task.h"
#include "main.h"
#define MODBUS_RECV_BUF AGILE_MODBUS_MAX_ADU_LENGTH

//读取状态枚举
enum {
    RS485_RECV_STATE_START = 0,
    RS485_RECV_STATE_WAIT_FIRST,
    RS485_RECV_STATE_WAIT_BYTE,
    RS485_RECV_STATE_FINISH
};

//写入状态枚举
enum {
    RS485_SEND_STATE_START = 0,
    RS485_SEND_STATE_WAIT,
    RS485_SEND_STATE_FINISH
};

enum {
    MODBUS_MODE_MASTER,
    MODBUS_MODE_MAX
};


struct global_attr {
    int modbus_mode;
    agile_modbus_rtu_t ctx_rtu;
    uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH];  //发送缓冲区
    uint8_t ctx_read_buf1[MODBUS_RECV_BUF];  //接受缓冲区
    uint8_t erase_flag;
    int modbus_step[MODBUS_MODE_MAX];  //状态机状态标志
    uint32_t modbus_total_cnt[MODBUS_MODE_MAX];  //装载计数
    uint32_t modbus_success_cnt[MODBUS_MODE_MAX];  //通讯成功计数
};
extern struct global_attr gbl_attr;

extern UART_HandleTypeDef huart4;
extern DMA_HandleTypeDef hdma_uart4_tx;
extern DMA_HandleTypeDef hdma_uart4_rx;
extern UART_HandleTypeDef huart5;
extern DMA_HandleTypeDef hdma_uart5_tx;
extern DMA_HandleTypeDef hdma_uart5_rx;

void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart);
int bll_communication(struct task_pcb *task);

#endif
